package mrusanov.ui

import javax.swing.{ JPanel, JButton }
import mrusanov.nation.{ EndOfSeasonEvent, BattleEvent, World }
import mrusanov.util.DateUtil
import mrusanov.localization.Localization
import java.awt.event.{ ActionEvent, ActionListener }
import mrusanov.util.swing.{ Event, EventConsumer }
import mrusanov.nation.EndOfWeekEvent

class NextEventButton(eventConsumer: EventConsumer[JPanel], world: World, panelBuilder: PanelBuilder) extends JButton {

	var event = world.userTeamEventToday
	setText()

	addActionListener(new ActionListener {
		def actionPerformed(e: ActionEvent) {
			event match {
				case Some(BattleEvent) => {
					val info = world.userTeamBattleToday.get
					info.battle.initAndPutPlayers()
					val event = Event(panelBuilder.battlePanel(info))
					eventConsumer.consumeEvent(event)
				}
				// TODO implement this
				case Some(EndOfSeasonEvent) => throw new UnsupportedOperationException
				case _ => {
					world.nextDay()
					eventConsumer.consumeEvent(Event(panelBuilder.calendar))
					event = world.userTeamEventToday
					if (event == None) {
						actionPerformed(e)
					}
					setText()
				}
			}
		}

	})

	private def setText() {
		event match {
			case Some(BattleEvent) => setText(Localization("nextEventButton.playGame"))
			case Some(EndOfSeasonEvent) => setText(Localization("nextEventButton.nextSeason"))
			case _ => setText(Localization("nextEventButton.nextEvent"))
		}
	}
}
